// miniprogram/pages/register/register.js
import {getUUID,getExt} from "../../utils/utils.js";
const app=getApp()
const db=wx.cloud.database();
const _=db.command;
Page({

  data: {
    temImgs: [],
    showRegBtn: false
  },

  onLoad: function (options) {
    this.initImageSize();
  },
  onSub: function(event){
    const that = this;
    this.setData({showRegBtn: true})
    const qyname=event.detail.value.qyname;
    db.collection("regesters").where({
      qyname: _.eq(qyname)
    }).get().then(res=>{
      if(res.data.length>0){
        wx.showToast({
              title: '账号已经存在了',
              icon: 'none'
            })
        this.setData({showRegBtn:false})
         
      }else{
 const password = event.detail.value.password;
    const location = event.detail.value.location;
    const organ = event.detail.value.organ;
    const contact=event.detail.value.contact;
    const phone=event.detail.value.phone;
    const mail=event.detail.value.mail;
    const about=event.detail.value.about;
    const deadline=event.detail.value.deadline;
    const saveObj={
      qyname: qyname,
     password: password,
     location: location,
     organ: organ,
     contact: contact,
     phone: phone,
     mail: mail,
     about: about,
     deadline: deadline,

    }
    wx.showLoading({
      title:'上传中...'
    })
    const fileIDList = [];
    if(that.data.temImgs.length>0){
      
      const today=new Date();
      const year=today.getFullYear();
      const mouth=today.getMonth()+1;
      //const day=today.getDate();
      that.data.temImgs.forEach((value,index)=>{
        wx.cloud.uploadFile({
          filePath: value,
          cloudPath: "zzImg/"+year+"/"+mouth+"/"
          //+day+"/"
          +getUUID()+"."+getExt(value),
          success: res=>{
            fileIDList.push(res.fileID);
           // if(fileIDList.length==that.data.temImgs.length){
              saveObj.images=fileIDList;
              that.publish(saveObj);
           // }
          }
        })
        
      })
      
    }else{
      // that.publish(saveObj);
      wx.showToast({
        title: '图片不能为空',
        icon: 'none'
      })
      this.setData({showRegBtn:false})
    }

      }
    })
    
    
   
   
  },
  publish: function(saveObj){
    
    wx.cloud.callFunction({
      name: "regis",
      data: {
        saveObj: saveObj
      },
      success:result=>{
        const _id=result.result._id;
        if(_id){
          wx.hideLoading();
          wx.showToast({
            icon: 'success',
            title: '上传成功! '
          });
          setTimeout(function(){
            wx.navigateBack({})
          },1000)
        }else{
          wx.showToast({
            title:'失败'
          })
        }
      }
    })

  },
  initImageSize: function(){
    const windowsWidth=wx.getSystemInfoSync().windowWidth;
    const containerWidth=windowsWidth-60;
    const imageSize=(containerWidth-2.5*3)/3;
    this.setData({
      imageSize: imageSize
    })
  },
  onImgAddTap :function(){
    const that=this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const temImgs=res.tempFilePaths;
        const oldImgs=that.data.temImgs;
        const newImgs=oldImgs.concat(temImgs);
        that.setData({
          temImgs: newImgs
        })
      },
    })
  },
  onCloseImgTap: function(event){
    const index=event.target.dataset.index;
    const temImgs=this.data.temImgs;
    temImgs.splice(index,1);
    this.setData({
      temImgs: temImgs
    })
  },
  onImgTap: function(ev){
    const that=this;
    const index=ev.target.dataset.index;
    const current=that.data.temImgs[index];
    wx.previewImage({
      urls: that.data.temImgs,
      current: current
    })
  }
})